<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="gongzi" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('') ">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template v-slot=" {data} ">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <span> {{ data.name }} </span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col> {{ data.manager }} </el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(data.id) ">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id) ">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id) ">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>

                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showVisible"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      width="45%"
    >
      <add-or-edit v-if="showVisible" :id="curId" :is-edit="isEdit" :orgin-list="originList" @success="hsuccess" @callOff="callOff" />
    </el-dialog>

  </div>
</template>
<script>
import { department, delDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils'
// 因为添加和编辑都是弹出dialog,所以把dialog封装成一个组件
import addOrEdit from './add-or-edit .vue'
export default {
  components: { addOrEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showVisible: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    // 获取部门列表
    this.departmentList()
  },
  methods: {
    async  departmentList() {
      const res = await department()
      // 删除第一个数据,公司名称
      res.data.depts.shift()
      // 高级写法,map把数据遍历,在形参解构,生成一个新数组,表单校验
      this.originList = res.data.depts.map(({ id, code, name, pid }) => ({ id, code, name, pid }))
      // 引入工具函数,把数据转化成树形结构
      this.list = tranListToTreeData(res.data.depts)
    },
    // 添加一个部门
    hShowAdd(id) {
    // 点击后打开dialog,把id传给子组件
      this.showVisible = true
      this.curId = id
      // 因为添加和编辑都是共用一个dialog组件,所以根据isEdit的布尔值来判断是添加还是编辑
      this.isEdit = false
    },
    hsuccess() {
      // 子组件做完添加或编辑后调用这个函数,关闭dialog,重新渲染项目列表
      this.showVisible = false
      this.departmentList()
    },
    callOff() {
      this.showVisible = false
    },
    hEdit(id) {
      // 编辑部门,把isEdit的状态改为true,显示弹窗,把id传给子组件
      this.isEdit = true
      this.showVisible = true
      this.curId = id
    },
    hDel(id) {
      // 提示用户是否要删除
      this.$confirm('真的要删除吗', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          // 删除部门的请求
          const res = await delDepartment(id)
          // 提示用户删除成功
          this.$message.success(res.message)
          // 重新显示数据
          this.departmentList()
        } catch (e) {
          this.$message.error(e.message)
        }
      })
        .catch(() => {})
    }
  }
}
</script>
